package com.tanhua.sso.controller;

import com.tanhua.sso.service.MyCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author feihao
 * @version 1.0
 * @date 2021/7/11 22:20
 */

@RestController
@RequestMapping("users")
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;


    /**
     * 发送短信验证码接口：
     *
     * @param token
     * @return
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity<Void> sendVerificationCode(@RequestHeader("Authorization") String token) {

        try {
            Boolean b = myCenterService.sendVerificationCode(token);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(null).build();
    }

    /**
     * 校验验证码接口
     *
     * @param mapParam
     * @param token
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity<Map<String, Object>> checkVerificationCode(@RequestBody Map<String, String> mapParam,
                                                                     @RequestHeader("Authorization") String token) {

        String code = mapParam.get("verificationCode");

        Map<String, Object> result = new HashMap<>();
        try {
            Boolean b = myCenterService.checkVerificationCode(code, token);
            result.put("verification", b);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(null).build();
    }


    /**
     * 保存新手机号：
     *
     * @param mapParam
     * @param token
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity<Void> saveNewPhone(@RequestBody Map<String, String> mapParam,
                                             @RequestHeader("Authorization") String token) {

        String newPhone = mapParam.get("phone");

        try {
            Boolean b = myCenterService.saveNewPhone(newPhone, token);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(null).build();
    }
}
